import connector.driver
from connector.driver import DatabaseDriver

# 根据书籍分类名查询分类id
def get_cate_id_by_cate_name(driver: DatabaseDriver, cate_name: str):
    return driver.query_one("select id from category where cate_name = '{}'".format(cate_name), log=True)

def get_cate_id_by_cate_name(driver: DatabaseDriver, cate_name: str) -> int:
    return driver.query_one("select id from category where cate_name like '%{}%'".format(cate_name), log=True)

def get_cate_by_id(driver: DatabaseDriver, cate_id: int) -> tuple:
    return driver.query_one("select * from category where id = {}".format(cate_id), log=True)

def get_cate_by_id(driver: DatabaseDriver, cate_id: int) -> tuple:
    return driver.query_one("select * from category where cate_id = {}".format(cate_id), log=True)

def update_cate(driver: DatabaseDriver, cate_id: int, cate_name: str) -> int:
    return driver.update("update category set cate_name = '{}' where cate_id = {}".format(cate_name, cate_id))

def add_cate(driver: DatabaseDriver, cate_name: str) -> int:
    return driver.insert("insert into category(cate_name) values ('{}')".format(cate_name))